// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef _CLIENT_COMMON_H_
#define _CLIENT_COMMON_H_

#include <string.h>
#include <stdio.h>
#include <libgen.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <libgen.h>
#include <unistd.h>
#include <map>
#include <vector>
#include <cstring>

#define WORKER_REPORT_HEARTBEAT 0x0001
#define CLOUD_PHONE_DEVICE_REQ 0x0201
#define CLOUD_PHONE_DEVICE_RSP 0x0202
#define WORKER_REPORT_STATUS_REQ 0x0203
#define WORKER_REPORT_STATUS_RSP 0x0204

const uint32_t CLOUD_PHONE_ID_LEN = 33;
const uint32_t IP_ADDR_LENGTH = 16;
const uint32_t UUID_LENGTH = 40;

const std::string REMOTE_SCHEDULING_DEFAULT_IP = "0.0.0.0";
const uint16_t REMOTE_SCHEDULING_DEFAULT_PORT = 65535;

enum RemoteEncodeType {
    CPU = 0x1,
    GPU = 0x2,
    ENCODE_TYPE_MAX
};

enum SchedulingResult {
    SUCCESS = 0x1,
    NO_MATCH = 0x2,
    NO_AVAILABLE = 0x3,
    RESULT_MAX
};

enum ReqRemoteResourceType {
    ENCODE,
    DECODE,
    RESOURCE_TYPE_MAX
};

enum ReqRemoteDataType {
    H264,
    H265,
    MIX,
    DATA_TYPE_MAX
};

enum ReqRemoteFlavorType {
    P720,
    P1080,
    FLAVOR_TYPE_MAX
};

typedef struct RemoteSchedulingReqS {
    uint8_t resourceType; // 转码类型 0 编码 1 解码
    uint8_t dataType;     // 数据类型 H264/H265
    uint8_t flavorType;   // 规格类型 720P/1080P
    char phoneId[CLOUD_PHONE_ID_LEN];
    uint8_t frameRate;    // 帧率类型 20/30/60
    uint8_t protocolVersion; // 调度协议版本
} __attribute__((packed)) RemoteSchedulingReqT;

typedef struct RemoteSchedulingRspS {
    uint8_t result;             // 0 表示请求失败, 1 表示请求成功
    char ipStr[IP_ADDR_LENGTH]; // 编解码集群中分配到的worker的IP
    uint16_t port;              // worker上具体的编解码实例对应的端口
    uint8_t encodeType;         // worker编码类型，0 CPU 1 GPU
} __attribute__((packed)) RemoteSchedulingRspT;

typedef struct WorkerStatusReqS {
    char workerUuid[UUID_LENGTH];
} WorkerStatusReqT;

typedef struct WorkerStatusRspS {
    char workerUuid[UUID_LENGTH];
    char workerIp[IP_ADDR_LENGTH];
    uint32_t port;
    uint32_t status;
    uint8_t encodeType;
    uint8_t resourceType;
    uint8_t dataType;
    uint8_t flavorType;
    uint8_t frameRate;
} __attribute__((packed)) WorkerStatusRspT;

static constexpr uint32_t WORKER_CAPACITY_RSP_SIZE = sizeof(WorkerStatusRspT);

class CommonClientPublic {
public:
    CommonClientPublic();

    ~CommonClientPublic();

public:
};

extern CommonClientPublic gClientInfo;
#endif